import React from 'react';
import {Icon, Picker, Tools} from "weaver-mobile";
import {getWeekOfYear} from "../../util";
import PropTypes from "prop-types";

const getLabel = Tools.getLabel;

/**
 * 自定义年月/季度/年picker
 * jiansong 2019/11/8
 */

/**
 * @param title 选择标题
 * @param initval 初始值
 * @param type 选择类型 1月度/2季度/3半年/4年度
 * @param onChange 改变事件
 * @param minyear 选择的最小年份
 * @param maxyear 选择的最大年份
 * @param weekText
 * @param totalWeek
 */
const WrPicker = ({title, initval = [], type, onChange, minyear = (new Date().getFullYear() - 10), maxyear = (new Date().getFullYear() + 10), weekText = "", totalWeek = 54}) => {
    return type && <span className="workrelate-picker-date-switch">
        <Icon onClick={() => changeLeft(type, initval, onChange, minyear, totalWeek)} type="left" size="xs"/>
        <Picker
            data={bulidData(type, minyear, maxyear, totalWeek)}
            title={title}
            cascade={false}
            value={getInit(initval, type)}
            onChange={onChange}
        >
            <span className="title">{buildTitle(initval, type, weekText)}</span>
        </Picker>
        <Icon onClick={() => changeRight(type, initval, onChange, maxyear, totalWeek)} type="right" size="xs"/>
    </span>
}

/**
 * 向左侧调节
 */
const changeLeft = (type, initval, onChange, minyear, totalWeek) => {
    if (type == 1) {
        if (initval[1] == 1) {
            if (initval[0] - 1 >= minyear) {
                onChange([initval[0] - 1, 12])
            }
        } else {
            onChange([initval[0], initval[1] - 1])
        }
    } else if (type == 2) {
        if (initval[1] == 1) {
            if (initval[0] - 1 >= minyear) {
                onChange([initval[0] - 1, 4])
            }
        } else {
            onChange([initval[0], initval[1] - 1])
        }
    } else if (type == 3 || type == 4) {
        if (initval[0] - 1 >= minyear) {
            onChange([initval[0] - 1, ""])
        }
    } else {
        if (initval[1] == 1) {
            if (initval[0] - 1 >= minyear) {
                onChange([initval[0] - 1, 52])
            }
        } else {
            onChange([initval[0], initval[1] - 1])
        }
    }
}

/**
 * 向右侧调节
 */
const changeRight = (type, initval, onChange, maxyear, totalWeek) => {
    if (type == 1) {
        if (initval[1] == 12) {
            if (initval[0] + 1 <= maxyear) {
                onChange([initval[0] + 1, 1])
            }
        } else {
            onChange([initval[0], initval[1] + 1])
        }
    } else if (type == 2) {
        if (initval[1] == 4) {
            if (initval[0] + 1 <= maxyear) {
                onChange([initval[0] + 1, 1])
            }
        } else {
            onChange([initval[0], initval[1] + 1])
        }
    } else if (type == 3 || type == 4) {
        if (initval[0] + 1 <= maxyear) {
            onChange([initval[0] + 1, ""])
        }
    } else {
        if (initval[1] == totalWeek) {
            if (initval[0] + 1 <= maxyear) {
                onChange([initval[0] + 1, 1])
            }
        } else {
            onChange([initval[0], initval[1] + 1])
        }
    }
}

/**
 * 获取初始值
 */
const getInit = (val, type) => {
    var date = new Date();
    if (type == 3 || type == 4) {
        val[0] = val[0] ? Number(val[0]) : date.getFullYear();
        return [val[0]];
    } else {
        val[0] = val[0] ? Number(val[0]) : date.getFullYear();
        val[1] = (type == 1 && !val[1]) ? date.getMonth() + 1 : Number(val[1]);
        val[1] = (type == 2 && !val[1]) ? getQuarterByMonth(date.getMonth()) : Number(val[1]);
        val[1] = (type == 5 && !val[1]) ? getWeekOfYear() : Number(val[1]);
        return val;
    }
}

const monthdata = {
    1: getLabel(25825, "1月"),
    2: getLabel(25826, "2月"),
    3: getLabel(25827, "3月"),
    4: getLabel(25828, "4月"),
    5: getLabel(25829, "5月"),
    6: getLabel(25830, "6月"),
    7: getLabel(25831, "7月"),
    8: getLabel(25832, "8月"),
    9: getLabel(25833, "9月"),
    10: getLabel(25834, "10月"),
    11: getLabel(25835, "11月"),
    12: getLabel(25836, "12月")
};
const seasondata = {
    1: getLabel(509092, "1季度"),
    2: getLabel(509093, "2季度"),
    3: getLabel(509094, "3季度"),
    4: getLabel(509095, "4季度")
};

/**
 * 构建标题
 */
const buildTitle = (val, type, weekText) => {
    if (type == 1) {
        return getLabel(508992, "{0}年").replace("{0}", val[0]) + monthdata[val[1]];
    } else if (type == 2) {
        return getLabel(508992, "{0}年").replace("{0}", val[0]) + seasondata[val[1]];
    } else if (type == 5) {
        return <span className="week">
            <span>{getLabel(508992, "{0}年").replace("{0}", val[0])}-{val[1]}{getLabel("1926", "周")}</span>
            <span className="week-text">{weekText}</span>
        </span>
    } else {
        return getLabel(508992, "{0}年").replace("{0}", val[0]);
    }
}

/**
 * 构建数据
 */
const bulidData = (type = 2, minyear, maxyear, totalWeek) => {
    var data = [];
    var year = [];
    for (let i = minyear; i < maxyear + 1; i++) {
        year.push({label: getLabel(508992, "{0}年").replace("{0}", i), value: i,})
    }
    if (type == 1) {//月份
        data[0] = year;
        data[1] = [];
        for (let i = 1; i < 13; i++) {
            data[1].push({label: monthdata[i], value: i});
        }
    } else if (type == 2) {//季度
        data[0] = year;
        data[1] = [];
        for (let i = 1; i < 5; i++) {
            data[1].push({label: seasondata[i], value: i});
        }
    } else if (type == 3 || type == 4) {
        data[0] = year;
    } else {
        data[0] = year;
        data[1] = [];
        for (let i = 1; i <= totalWeek; i++) {
            data[1].push({label: getLabel(513721, "{0}周").replace("{0}", i), value: i});
        }
    }
    return data;
}

/**
 * 根据月份获取季度
 * @param month 月份
 * @returns {number} 当前季度
 */
const getQuarterByMonth = (month) => {
    if (month >= 0 && month <= 2) {
        return 1;
    } else if (month >= 3 && month <= 5) {
        return 2;
    } else if (month >= 6 && month <= 8) {
        return 3;
    } else if (month >= 9 && month <= 11) {
        return 4;
    }
}

WrPicker.propTypes = {
    title: PropTypes.string,
    initval: PropTypes.array,
    type: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
    onChange: PropTypes.func,
    minyear: PropTypes.number,
    maxyear: PropTypes.number,
}
export default WrPicker;